package com.oa.yxg.action;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.oa.domain.Dept;
import com.oa.domain.Emp;
import com.oa.domain.TbGroups;
import com.oa.yxg.service.YxgService;
import com.opensymphony.xwork2.ActionSupport;

public class MsgAction extends ActionSupport {

	private YxgService yxgService;// 服务层

	private List<TbGroups> tgroups;// 通讯录显示个人分组用

	private List<Dept> depts;// 通讯录显示企业分组用

	private TbGroups tbGroups;// 添加分组用

	// 测试用用户 不用session
	private Emp emp;

	/**
	 * 显示分组
	 * 
	 * @return
	 */
	public String getGroups() {
		// 从session中取得员工
		HttpSession session = ServletActionContext.getRequest().getSession();

		emp = new Emp();
		emp.setEmpno(1003l);
		emp = yxgService.getEmpByExmple(emp).get(0);

		System.out.println("empname:" + emp.getEname());

		session.setAttribute("emp", emp);
		// session.getAttribute("emp");

		/** 得到私人分组 **/
		TbGroups tGroups = new TbGroups();
		tGroups.setEmpno(1003l);
		tgroups = yxgService.getTbGrupsByExample(tGroups);
		
		System.out.println("tgroups.size():" + tgroups.size());
		
		//将个人分组放入session
		session.setAttribute("tgroups", tgroups);
		
		/** 得到企业通讯录（部门分组） **/
		depts = yxgService.getDept();
		//将个人分组放入session
		session.setAttribute("depts", depts);

		System.out.println("depts.size():" + depts.size());
		return SUCCESS;
	}

	/**
	 * 添加分组
	 * 
	 * @return
	 */
	public String addGroup() {
		// 从session中取得员工
		HttpSession session = ServletActionContext.getRequest().getSession();
		emp = (Emp) session.getAttribute("emp");
		//查询分组
		tbGroups.setEmpno(emp.getEmpno());
		// 这部怎么回事？
		// 不能自动添加主键------------------------------------------------------??
		tbGroups.setGroupid(1001l);

		if (tbGroups.getGname() == null || "".equals(tbGroups.getGname())) {
			System.out.println("组名取值为空");
		} else {
			System.out.println("组名：" + tbGroups.getGname());
		}

		yxgService.addTbGroup(tbGroups);
		System.out.println("增加");
		return "ADDGROUP";
	}

	/**
	 * 添加联系人
	 * 
	 * @param yxgServ
	 */
	public String addGroupEmp() {
		// 从session中取得员工
		HttpSession session = ServletActionContext.getRequest().getSession();
		emp = (Emp) session.getAttribute("emp");

		return "ADDGROUPEMP";
	}

	public void setYxgService(YxgService yxgService) {
		this.yxgService = yxgService;
	}

	public void setTgroups(List<TbGroups> tgroups) {
		this.tgroups = tgroups;
	}

	public List<TbGroups> getTgroups() {
		return tgroups;
	}

	public void setDepts(List<Dept> depts) {
		this.depts = depts;
	}

	public List<Dept> getDepts() {
		return depts;
	}

	public void setTbGroups(TbGroups tbGroups) {
		this.tbGroups = tbGroups;
	}

	public TbGroups getTbGroups() {
		return tbGroups;
	}

	public Emp getEmp() {
		return emp;
	}

	public void setEmp(Emp emp) {
		this.emp = emp;
	}

}
